<template>
  <div class="position-container">
    <PositionAdd ref="position-add"></PositionAdd>
    <PositionEdit ref="position-edit" :pos-id="posId"></PositionEdit>
    <el-button @click.native="handleAddButtonClick" style="margin-bottom: 10px" type="primary" icon="el-icon-plus">职位添加</el-button>
    <el-table
      :data="renderData.filter(data => !search || data.positionName.toLowerCase().includes(search.toLowerCase()))"
      style="width: 100%"
      border
      :header-cell-style="{backgroundColor: '#eee'}"
    >
      <el-table-column
        label="公司Logo"
        prop="companyLogo"
        width="100"
      >
        <template v-slot:default="scope">
          <img width="50" height="50" :src="`http://localhost:3000/uploads/${scope.row.companyLogo}`" alt="">
        </template>
      </el-table-column>
      <el-table-column
        label="公司名称"
        prop="companyName">
      </el-table-column>
      <el-table-column
        label="职位名称"
        prop="positionName">
      </el-table-column>
      <el-table-column
        label="城市"
        prop="city">
      </el-table-column>
      <el-table-column
        label="岗位薪资"
        prop="salary">
      </el-table-column>
      <el-table-column
        label="发布时间"
        prop="createTime">
      </el-table-column>
      <el-table-column
        align="center"
        width="150"
      >
        <template slot="header" slot-scope="scope">
          <el-input
            v-model="search"
            size="mini"
            placeholder="输入关键字搜索"/>
        </template>
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="tableData.length"
      @current-change="handlePageChange"
      :page-size="pageSize"
      :current-page="currentPage"
      :style="{margin: '10px 0', 'text-align': 'right'}"
    >
    </el-pagination>
  </div>
</template>

<script>
import { list } from '@/api/positions'
import PositionAdd from './PositionAdd'
import PositionEdit from './PositionEdit'
import { remove } from '@/api/positions'
export default {
  components: {
    PositionAdd,
    PositionEdit
  },

  data() {
    return {
      tableData: [],
      search: '',
      currentPage: 1,
      posId: "0"
    }
  },

  computed: {
    renderData() {
      let start = (this.currentPage - 1) * this.pageSize
      return this.tableData.slice(start, start + this.pageSize)
    }
  },

  created() {
    this.pageSize = 10
  },

  async mounted() {
    this.loadData()
  },

  methods: {
    async loadData() {
      let result = await list()
      this.tableData = result.data
    },

    handlePageChange(currentPage) {
      this.currentPage = currentPage
    },

    handleAddButtonClick() {
      this.$refs['position-add'].dialogFormVisible = true
    },

    handleEdit(index, row) {
      this.$refs['position-edit'].dialogFormVisible = true
      this.posId = row._id
    },

    async handleDelete(index, row) {
      let result = await remove({
        id: row._id
      })

      if (result.data.ret) {
        this.loadData()
      }
    }
  },
}
</script>

<style lang="scss" scoped>
.position-container {
  padding: 20px
}
</style>